﻿#include "svc_forward_from_base.h"

#include "asio/asio_server.h"  // for HandleRpcRequest()
#include "game_clt_id.h"  // for CGameCltId
#include "pb/svr/forward_from_base.pb.h"  // for ForwardRequest
#include "log.h"
#include "rpc/rpc_call_context.h"  // for IsForwarded()
#include "rpc/rpc_helper.h"  // for ParseMsgFromStr()

const char LOG_NAME[] = "CSSvcForwardFromBase";

CSSvcForwardFromBase::CSSvcForwardFromBase(CAsioServer& rServer)
	: m_rServer(rServer)
{
	RegisterMethod("Forward",
		[this](const CRpcCallContext& ctx, const std::string& sContent) {
			Forward(ctx, sContent);
		});
}

void CSSvcForwardFromBase::Forward(const CRpcCallContext& ctx,
	const std::string& sContent)
{
	if (ctx.IsForwarded())
	{
		LOG_ERROR("Double forward.");
		return;
	}

	svr::ForwardRequest fwdReq;
	if (!RpcHelper::ParseMsgFromStr(sContent, fwdReq))
		return;
	LOG_DEBUG(Fmt("Forward, %s.%s") % fwdReq.service() % fwdReq.method());

	const svr::ForwardRequest::GameCltId& gameCltId = fwdReq.game_clt_id();
	CRpcCallContext ctx2(ctx, CGameCltId(gameCltId.base_svr_id(),
		gameCltId.base_rpc_clt_id()));
	m_rServer.HandleRpcRequest(ctx2, fwdReq.service(), fwdReq.method(),
		fwdReq.content());
}
